package com.zhazha.earlybird.dao;


import com.zhazha.earlybird.beans.UserInfo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;



public class UserDAO {

	private SQLiteDatabase mDatabase;
	private static final String USER_TABLE = "user";
	
	private static final String USER_ID 				= "id";
	private static final String USER_EMAIL				= "email";
	private static final String USER_PASSWORD			= "password";
	private static final String USER_TYPE				= "user_type";
	
	private static final int USER_ID_INDEX 				= 0;
	private static final int USER_EMAIL_INDEX			= 1;
	private static final int USER_PASSWORD_INDEX		= 2;
	private static final int USER_TYPE_INDEX			= 3;
	
	public UserDAO(SQLiteDatabase db) {
		mDatabase = db;
	}
	
	
	public boolean insert(UserInfo user) {
		
		try {
			ContentValues cv = new ContentValues();
			cv.put(USER_ID, 			user.getId());
			cv.put(USER_EMAIL, 		user.getEmail());
			cv.put(USER_PASSWORD, 		user.getPassword());
			cv.put(USER_TYPE, user.getType());
			
			this.mDatabase.insert(USER_TABLE, null, cv);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean delete(int id) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.delete(USER_TABLE, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(int id, ContentValues cv) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.update(USER_TABLE, cv, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public UserInfo queryById(int id) {
		UserInfo user = null;
		
		try {
			String selection = "id=?";
			String[] selectionArgs = {String.valueOf(id)};
			Cursor c = this.mDatabase.query(USER_TABLE, null, selection, selectionArgs, null, null, null);
			
			if(c.moveToFirst()) {
				user = new UserInfo();
				user.setId(c.getInt(USER_ID_INDEX));
				user.setEmail(c.getString(USER_EMAIL_INDEX));
				user.setPassword(c.getString(USER_PASSWORD_INDEX));
				user.setType(c.getInt(USER_TYPE_INDEX));
			}
			
			return user;
			
		}catch (Exception e) {
			e.printStackTrace();
			return user;
		}
	}
	
	public UserInfo queryByEmail(String email) {
		UserInfo user = null;
		
		try {
			String selection = "email=?";
			String[] selectionArgs = {email};
			Cursor c = this.mDatabase.query(USER_TABLE, null, selection, selectionArgs, null, null, null);
			
			if(c.moveToFirst()) {
				user = new UserInfo();
				user.setId(c.getInt(USER_ID_INDEX));
				user.setEmail(c.getString(USER_EMAIL_INDEX));
				user.setPassword(c.getString(USER_PASSWORD_INDEX));
				user.setType(c.getInt(USER_TYPE_INDEX));
			}
			
			return user;
			
		}catch (Exception e) {
			e.printStackTrace();
			return user;
		}
	}
}
